/**
 * 
 */
package com.firebird.oms.web.dwr.bm;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.firebird.base.domain.Page;
import com.firebird.oms.constants.AuthConstants;
import com.firebird.oms.constants.BusiConstants;
import com.firebird.oms.domain.bm.TibmOrderQueue;
import com.firebird.oms.domain.sm.TismArea;
import com.firebird.oms.domain.sm.TismCombo;
import com.firebird.oms.domain.sm.TismStaticValue;
import com.firebird.oms.exception.OmsRuntimeException;
import com.firebird.oms.service.bm.OrderService;
import com.firebird.oms.service.sm.AuthService;
import com.firebird.oms.service.sm.ComboService;
import com.firebird.oms.service.sm.CompanyService;
import com.firebird.oms.service.sm.StaticService;
import com.firebird.oms.user.UserDetails;
import com.firebird.oms.util.SeqCreatorFactory;
import com.firebird.oms.web.base.BaseDwr;

/**
 * @author qiuxj 创建于 2009-11-28
 *
 */
public class OrderQueueDwr extends BaseDwr {
	
	private OrderService orderService;
	private AuthService authService;
	private CompanyService companyService;
	private ComboService comboService;
	private StaticService staticService;
	
	/**
	 * 创建订单队列
	 * @param bmOrderQueue
	 */
	public void createOrderQueue(TibmOrderQueue bmOrderQueue) {
		UserDetails user = super.getCurrentUser();
		bmOrderQueue.setCreateStaff(user.getStaffId());
		bmOrderQueue.setCreateTime(new Date());
		SeqCreatorFactory seqCreator = new SeqCreatorFactory();
		bmOrderQueue.setOrderId(seqCreator.getZbSeq());
		orderService.createOrderQueue(bmOrderQueue);
	}
	
	/**
	 * 更新订单队列
	 * @param bmOrderQueue
	 */
	public void updateOrderQueue(TibmOrderQueue bmOrderQueue) {
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() > AuthConstants.WORKER_LEVEL_SUPER){
			throw new OmsRuntimeException("权限不足，不能修改！");
		}
		
		bmOrderQueue.setModifyStaff(user.getStaffId());
		bmOrderQueue.setModifyTime(new Date());
		orderService.updateOrderQueue(bmOrderQueue);
	}
	
	/**
	 * 根据订单号获取订单信息
	 * @param orderId
	 * @return
	 */
	public TibmOrderQueue getOrderQueue(String orderId) {
		return orderService.getOrderQueue(orderId);
	}
	
	/**
	 * 获取订单列表 只能获取自己以及下属员工的订单
	 * @param filter
	 * @return
	 */
	public List<TibmOrderQueue> findOrderQueue(Map<String, String> filter) {
		List<TibmOrderQueue> orders = orderService.findOrderQueue(filter);
		if(orders.isEmpty()){
			return orders;
		}
		
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() != AuthConstants.WORKER_LEVEL_ADMIN){
			List<Integer> staffIds = authService.getChildStaff(user.getStaffId());
			for (Iterator<TibmOrderQueue> it = orders.iterator(); it.hasNext();) {
				TibmOrderQueue order = it.next();
				if (!staffIds.contains(order.getCreateStaff())) {
					it.remove();
				}
				
				TismArea area = companyService.getArea(order.getAreaId());
				area.setAreaName(area != null?area.getAreaName():"");
			}
		}
		return orders;
	}
	
	/**
	 * 获取分页订单列表 只能获取自己以及下属员工的订单
	 * @param filterMap
	 * @param likeMap
	 * @param sortMap
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page pageOrderQueue(Map<String, String> filterMap, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize){
		Page page = orderService.pageOrderQueue(filterMap, likeMap, sortMap, pageNo, pageSize);
		if(page.getResult().isEmpty()){
			return page;
		}
		
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() != AuthConstants.WORKER_LEVEL_ADMIN){
			List<Integer> staffIds = authService.getChildStaff(user.getStaffId());
			for (Iterator<TibmOrderQueue> it = page.getResult().iterator(); it.hasNext();) {
				TibmOrderQueue order = it.next();
				if (!staffIds.contains(order.getCreateStaff())) {
					it.remove();
				}
			}
		}
		
		for(TibmOrderQueue order : (List<TibmOrderQueue>)page.getResult()){
			TismArea area = companyService.getArea(order.getAreaId());
			order.setAreaName(area != null?area.getAreaName():"");
			
			TismCombo combo = comboService.getCombo(order.getComboId());
			order.setComboName(combo != null?combo.getComboName():"");
			
			Map<String, String> filter = new HashMap<String, String>();
			filter.put("staticTypeId", BusiConstants.STATIC_TYPE_PROD_TYPE);
			filter.put("staticValue", order.getProdType().toString());
			List<TismStaticValue> staticValue = staticService.findStaticValue(filter);
			order.setProdTypeDesc(staticValue != null && staticValue.size() > 0?staticValue.get(0).getStaticDesc():"");
		}
		
		return page;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public void setAuthService(AuthService authService) {
		this.authService = authService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public void setComboService(ComboService comboService) {
		this.comboService = comboService;
	}

	public void setStaticService(StaticService staticService) {
		this.staticService = staticService;
	}

}
